Jquery链不工作在我的HTML与每个函数
jquery chaining not working in my html with each function
这是我的html
<div class="productcheck">
<div class="expenserow1">
<select name="product[]" class='product_name'>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
<div class="expenserow2">
<input type="text" name="productpercent[]" size=3 maxlength=3 value="100">
</div>
</div>
<div class="productcheck">
<div class="expenserow1">
<select name="product[]" class='product_name'>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
<div class="expenserow2">
<input type="text" name="productpercent[]" size=3 maxlength=3 value="100">
</div>
</div>
<div class="productcheck">
<div class="expenserow1">
<select name="product[]" class='product_name'>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
<div class="expenserow2">
<input type="text" name="productpercent[]" size=3 maxlength=3 value="100">
</div>
</div>
JS:
<script>
$(document).ready(function(){
$(".productcheck .expenserow2 input[type='text']").each(function() {
var product_percentage=$(this).val();
var product_name=$(this).closest('.product_name:selected').val();
console.log(product_percentage);
});
});
</script>
我得到product_percentage值正确,但不能得到product_name值
您需要使用正确的选择器来选择元素。用途:
$(this).parent().prev().find('.product_name').val();
演示工作问题是你的选择器。product_name
不是文本框的父元素,所以你的代码不能工作。
var product_name = $(this)
.closest('.productcheck') //traverse to parent productcheck
.find('.product_name').val(); //Use .find() to search product_name You don't need selected here
相关文章:
- HTML表单提交时未执行外部函数
- 如何将输入(type=text)从html表单传递到javascript函数
- 从html创建一个指令,该指令按类名应用函数
- 将JavaScript函数与HTML分离
- 使用html表单中的参数调用JavaScript函数
- 从HTML调用typescript文件中的函数
- 如果使用javascript函数屏幕太小,我该如何更改HTML文件的背景色
- 从Chrome扩展名中的popup.html文件在background.js文件中运行一个函数
- 如何在HTML元素上创建函数,而不是将元素作为参数传递
- php函数的ajax html$_POST值返回null
- 函数jquery.html()不提供数据属性集值
- HTML 按钮点击函数无法使用 jQuery 变量作为参数
- 从index.html调用函数,该函数无限循环
- HTML如何根据javascript函数的返回值限制文本输入
- 如何使用Javascript/Jquery/HTML5函数将HTML页面捕获为图像
- 将 HTML 添加到 Javascript IF 函数
- 在函数 HTML 中运行 getelementbyid
- 如何在 onclick 函数 HTML 中传递字符串
- jQuery/MVC3函数html onclick只工作一次
- 每次点击调用函数(HTML包括帧)