【问题标题】:Jquery to target .this element not globalJquery 的目标 .this 元素不是全局的
【发布时间】:2016-06-07 09:44:36
【问题描述】:

我正在使用以下代码(不是我的),我正在尝试让增量按钮仅针对与它们相关的表单。但是,当您有多个如下所示的表单时,增量框会增加页面上的两个(所有)输入框。

如何仅定位我正在使用的表单?

我确定这是一个简单的解决方法,但我无法弄清楚!请帮忙!

提前一百万谢谢!

艾略特。

<form id='myform' method='POST' action='#'>
    <input type='button' value='-' class='qtyminus' field='quantity' />
    <input type='text' name='quantity' value='0' class='qty' />
    <input type='button' value='+' class='qtyplus' field='quantity' />
</form>
<form id='myform' method='POST' action='#'>
    <input type='button' value='-' class='qtyminus' field='quantity' />
    <input type='text' name='quantity' value='0' class='qty' />
    <input type='button' value='+' class='qtyplus' field='quantity' />
</form>
<script> 
$(document).ready(function(){
    // This button will increment the value
    $('.qtyplus').click(function(e){
        // Stop acting like a button
        e.preventDefault();
        // Get the field name
        fieldName = $(this).attr('field');
        // Get its current value
        var currentVal = parseInt($('input[name='+fieldName+']').val());
        // If is not undefined
        if (!isNaN(currentVal)) {
            // Increment
            $('input[name='+fieldName+']').val(currentVal + 1);
        } else {
            // Otherwise put a 0 there
            $('input[name='+fieldName+']').val(0);
        }
    }); 
    // This button will decrement the value till 0
    $(".qtyminus").click(function(e) {
        // Stop acting like a button
        e.preventDefault();
        // Get the field name
        fieldName = $(this).attr('field');
        // Get its current value
        var currentVal = parseInt($('input[name='+fieldName+']').val());
        // If it isn't undefined or its greater than 0
        if (!isNaN(currentVal) && currentVal > 0) {
            // Decrement one
            $('input[name='+fieldName+']').val(currentVal - 1);
        } else {
            // Otherwise put a 0 there
            $('input[name='+fieldName+']').val(0);
        }
    });
});
</script>

【问题讨论】:

  • 这和你的问题无关,但是你不应该给多个元素赋予相同的id('myform')。

标签: jquery forms input


【解决方案1】:

您可以通过这种方式获取该字段:

var $field = $(this).parent().find('input[name='+fieldName+']');

这将从单击的元素上移一个元素,然后从可用的子元素中搜索您的字段。

jQuery(document).ready(function(){
    // This button will increment the value
    $('.qtyplus').click(function(e){
        // Stop acting like a button
        e.preventDefault();
        // Get the field name
        var fieldName = $(this).attr('field');
        var $field = $(this).parent().find('input[name='+fieldName+']');
        // Get its current value
        var currentVal = parseInt($field.val());
        // If is not undefined
        if (!isNaN(currentVal)) {
            // Increment
            $field.val(currentVal + 1);
        } else {
            // Otherwise put a 0 there
            $field.val(0);
        }
    }); 
    // This button will decrement the value till 0
    $(".qtyminus").click(function(e) {
        // Stop acting like a button
        e.preventDefault();
        // Get the field name
        var fieldName = $(this).attr('field');
        var $field = $(this).parent().find('input[name='+fieldName+']');
        // Get its current value
        var currentVal = parseInt($field.val());
        // If it isn't undefined or its greater than 0
        if (!isNaN(currentVal) && currentVal > 0) {
            // Decrement one
            $field.val(currentVal - 1);
        } else {
            // Otherwise put a 0 there
            $field.val(0);
        }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<form id='myform' method='POST' action='#'>
    <input type='button' value='-' class='qtyminus' field='quantity' />
    <input type='text' name='quantity' value='0' class='qty' />
    <input type='button' value='+' class='qtyplus' field='quantity' />
</form>
<form id='myform' method='POST' action='#'>
    <input type='button' value='-' class='qtyminus' field='quantity' />
    <input type='text' name='quantity' value='0' class='qty' />
    <input type='button' value='+' class='qtyplus' field='quantity' />
</form>

【讨论】:

  • 考虑使用.closest('form')而不是.parent(),以便在表单内的html结构发生变化时保持工作
【解决方案2】:

要仅匹配当前表单中的元素,请替换所有出现的:

$('input[name='+fieldName+']')

与:

$(this).closest('form').find('input[name='+fieldName+']')

最好将其保存为参考并使用它:

var $field = $(this).closest('form').find('input[name='+fieldName+']');

// Now you can use...
$field.val();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 1970-01-01
    • 2011-08-07
    • 2014-07-31
    • 2011-05-04
    • 2011-01-20
    • 1970-01-01
    相关资源
    最近更新 更多