jquery实现div单选?jquery添加div
本篇文章给大家谈谈jquery实现div单选,以及jquery添加div对应的知识点,文章可能有点长,但是希望大家可以阅读完,增长自己的知识,最重要的是希望对各位有所帮助,可以解决了您的问题,不要忘了收藏本站喔。
jQuery如何获取选中单选按钮radio的值
简单来说:var val=$('input[name="sex"]:checked').val();
使用jquery获取radio的值,最重要的是掌握jquery选择器的使用,在一个表单中我们通常是要获取被选中的那个radio项的值,所以要加checked来筛选。
扩展资料:
jQuery是一个快速、简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaScript框架)。jQuery设计的宗旨是“write Less,Do More”,即倡导写更少的代码,做更多的事情。它封装JavaScript常用的功能代码,提供一种简便的JavaScript设计模式,优化HTML文档操作、事件处理、动画设计和Ajax交互。
如何用Jquery控制单选按钮点击否然后隐藏其他文本框
思路:在设计HTML结构时,将单选按钮和对应的文本框关联(例如单选按钮的value值对应了文本框的位置)。然后在单选按钮的的单击事件处理函数中,隐藏相应的文本框。实例演示如下:
1、HTML结构:注意单选框的value值是和文本框在div中的位置对应的
<input type="radio" name="test" value="0" checked>第一组
<input type="radio" name="test" value="1">第二组
<div id="test">
<div>第一组文本框:<input type="text"/></div>
<div>第二组文本框:<input type="text"/><input type="text"/></div>
</div>
2、jquery代码
$("#test div:last").hide();//先隐藏第二组文本框
$(":radio").click(function(){
var num=$(this).val();//当前按钮的value值对应需要显示文本框的位置
$("#test div").hide();//先隐藏所有文本框
$("#test div").eq(num).show();//然后显示对应的文本框
});
3、效果演示
WebDriver到底怎么用
1.2用webdriver打开一个浏览器
我们常用的浏览器有firefox和IE两种,firefox是selenium支持得比较成熟的浏览器。但是做页面的测试,速度通常很慢,严重影
响持续集成的速度,这个时候建议使用HtmlUnit,不过HtmlUnitDirver运行时是看不到界面的,对调试就不方便了。使用哪种浏览器,可以
做成配置项,根据需要灵活配置。
打开firefox浏览器:
//Create a newinstance of the Firefox driver
WebDriver driver= newFirefoxDriver();
打开IE浏览器
//Create a newinstance of the Internet Explorer driver
WebDriver driver= newInternetExplorerDriver();
打开HtmlUnit浏览器
//Createa new instance of the Internet Explorer driver
WebDriverdriver= new HtmlUnitDriver();
1.3打开测试页面
对页面对测试,首先要打开被测试页面的地址(如:),web driver提供的get方法可以打开一个页面:
// And now use thedriver to visit Google
driver.get("");
1.4 GettingStarted
package org.openqa.selenium.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Selenium2Example{
public static voidmain(String[] args){
// Create a newinstance of the Firefox driver
// Notice that theremainder of the code relies on the interface,
// not the implementation.
WebDriver driver= newFirefoxDriver();
// And now use this tovisit Google
driver.get("");
// Alternatively thesame thing can be done like this
// driver.navigate().to("");
// Find the text inputelement by its name
WebElement element=driver.findElement(By.name("q"));
// Enter something tosearch for
element.sendKeys("Cheese!");
// Now submit the form.WebDriver will find the form for us from the element
element.submit();
// Check the title ofthe page
System.out.println("Page title is:"+ driver.getTitle());
// Google's search isrendered dynamically with JavaScript.
// Wait for the pageto load, timeout after 10 seconds
(newWebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>(){
public Booleanapply(WebDriver d){
returnd.getTitle().toLowerCase().startsWith("cheese!");
}
});
// Should see:"cheese!- Google Search"
System.out.println("Page title is:"+ driver.getTitle());
//Close the browser
driver.quit();
}
}
第2章 Webdirver对浏览器的支持
2.1 HtmlUnit Driver
优点:HtmlUnit Driver不会实际打开浏览器,运行速度很快。对于用FireFox等浏览器来做测试的自动化测试用例,运行速度通常很慢,HtmlUnit Driver无疑是可以很好地解决这个问题。
缺点:它对JavaScript的支持不够好,当页面上有复杂JavaScript时,经常会捕获不到页面元素。
使用:
WebDriver driver= new HtmlUnitDriver();
2.2 FireFox Driver
优点:FireFox Dirver对页面的自动化测试支持得比较好,很直观地模拟页面的操作,对JavaScript的支持也非常完善,基本上页面上做的所有操作FireFox Driver都可以模拟。
缺点:启动很慢,运行也比较慢,不过,启动之后Webdriver的操作速度虽然不快但还是可以接受的,建议不要频繁启停FireFox Driver。
使用:
WebDriver driver= new FirefoxDriver();
Firefox profile的属性值是可以改变的,比如我们平时使用得非常频繁的改变useragent的功能,可以这样修改:
FirefoxProfile profile= new FirefoxProfile();
profile.setPreference("general.useragent.override","some UAstring");
WebDriver driver= new FirefoxDriver(profile);
2.3 InternetExplorer Driver
优点:直观地模拟用户的实际操作,对JavaScript提供完善的支持。
缺点:是所有浏览器中运行速度最慢的,并且只能在Windows下运行,对CSS以及XPATH的支持也不够好。
使用:
WebDriver driver= new InternetExplorerDriver();
第3章使用操作
3.1如何找到页面元素
Webdriver的findElement方法可以用来找到页面的某个元素,最常用的方法是用id和name查找。下面介绍几种比较常用的方法。
3.1.1 By ID
假设页面写成这样:
<input type="text" name="passwd"id="passwd-id"/>
那么可以这样找到页面的元素:
通过id查找:
WebElement element= driver.findElement(By.id("passwd-id"));
3.1.2 By Name
或通过name查找:
WebElement element= driver.findElement(By.name("passwd"));
3.1.3 By XPATH
或通过xpath查找:
WebElement element=driver.findElement(By.xpath("//input[@id='passwd-id']"));
3.1.4 By Class Name
假设页面写成这样:
<div
class="cheese"><span>Cheddar</span></div><divclass="cheese"><span>Gouda</span></div>
可以通过这样查找页面元素:
List<WebElement>cheeses= driver.findElements(By.className("cheese"));
3.1.5 By Link Text
假设页面元素写成这样:
<arel="external nofollow" href="">cheese</a>>
那么可以通过这样查找:
WebElement cheese=driver.findElement(By.linkText("cheese"));
3.2如何对页面元素进行操作
找到页面元素后,怎样对页面进行操作呢?我们可以根据不同的类型的元素来进行一一说明。
3.2.1输入框(text field or textarea)
找到输入框元素:
WebElement element= driver.findElement(By.id("passwd-id"));
在输入框中输入内容:
element.sendKeys(“test”);
将输入框清空:
element.clear();
获取输入框的文本内容:
element.getText();
3.2.2下拉选择框(Select)
找到下拉选择框的元素:
Select select= new Select(driver.findElement(By.id("select")));
选择对应的选择项:
select.selectByVisibleText(“mediaAgencyA”);
或
select.selectByValue(“MA_ID_001”);
不选择对应的选择项:
select.deselectAll();
select.deselectByValue(“MA_ID_001”);
select.deselectByVisibleText(“mediaAgencyA”);
或者获取选择项的值:
select.getAllSelectedOptions();
select.getFirstSelectedOption();
3.2.3单选项(Radio Button)
找到单选框元素:
WebElement bookMode=driver.findElement(By.id("BookMode"));
选择某个单选项:
bookMode.click();
清空某个单选项:
bookMode.clear();
判断某个单选项是否已经被选择:
bookMode.isSelected();
3.2.4多选项(checkbox)
多选项的操作和单选的差不多:
WebElement checkbox=driver.findElement(By.id("myCheckbox."));
checkbox.click();
checkbox.clear();
checkbox.isSelected();
checkbox.isEnabled();
3.2.5按钮(button)
找到按钮元素:
WebElement saveButton= driver.findElement(By.id("save"));
点击按钮:
saveButton.click();
判断按钮是否enable:
saveButton.isEnabled();
3.2.6左右选择框
也就是左边是可供选择项,选择后移动到右边的框中,反之亦然。例如:
Select lang= new Select(driver.findElement(By.id("languages")));
lang.selectByVisibleText(“English”);
WebElement addLanguage=driver.findElement(By.id("addButton"));
addLanguage.click();
3.2.7弹出对话框(Popup dialogs)
Alert alert= driver.switchTo().alert();
alert.accept();
alert.dismiss();
alert.getText();
3.2.8表单(Form)
Form中的元素的操作和其它的元素操作一样,对元素操作完成后对表单的提交可以:
WebElement approve= driver.findElement(By.id("approve"));
approve.click();
或
approve.submit();//只适合于表单的提交
3.2.9上传文件(Upload File)
上传文件的元素操作:
WebElement adFileUpload= driver.findElement(By.id("WAP-upload"));
String filePath="C:\test\\uploadfile\\media_ads\\test.jpg";
adFileUpload.sendKeys(filePath);
3.2.10 Windows和 Frames之间的切换
一般来说,登录后建议是先:
driver.switchTo().defaultContent();
切换到某个frame:
driver.switchTo().frame("leftFrame");
从一个frame切换到另一个frame:
driver.switchTo().frame("mainFrame");
切换到某个window:
driver.switchTo().window("windowName");
3.2.11拖拉(Drag andDrop)
WebElement element=driver.findElement(By.name("source"));
WebElement target= driver.findElement(By.name("target"));
(new Actions(driver)).dragAndDrop(element, target).perform();
3.2.12导航(Navigationand History)
打开一个新的页面:
driver.navigate().to("");
通过历史导航返回原页面:
driver.navigate().forward();
driver.navigate().back();
好了,关于jquery实现div单选和jquery添加div的问题到这里结束啦,希望可以解决您的问题哈!