Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在jquery中使用类获取下一个输入元素id?_Javascript_Jquery - Fatal编程技术网

Javascript 如何在jquery中使用类获取下一个输入元素id?

Javascript 如何在jquery中使用类获取下一个输入元素id?,javascript,jquery,Javascript,Jquery,我必须使用jquery在第一个文本框的更改事件中获取下一个文本框id。 我怎么能得到那个。我已经使用了next(),但它没有按我所希望的那样工作 这是我的密码 <table> <tbody> <tr> <td> <p>PF Value</p><br> </td> <td

我必须使用jquery在第一个文本框的更改事件中获取下一个文本框id。 我怎么能得到那个。我已经使用了next(),但它没有按我所希望的那样工作

这是我的密码

<table>
    <tbody>
        <tr>
            <td>
                <p>PF Value</p><br>
            </td>
            <td>
                <span id="ctl00_ctl00_bcr_bcr_TextBox1" class="txtfill" style="display: inline-block;">
                    <input name="ctl00$ctl00$bcr$bcr$TextBox1$textbox1" class="txtSec80CCD" value="0" id="ctl00_ctl00_bcr_bcr_TextBox1_textbox1" onblur="calcDedSecCCD(this)"  style="width: 70px;" type="text">
                    <span id="ctl00_ctl00_bcr_bcr_TextBox1_valCustom" style="color: Red; display: none;">*</span>
                    <div id="ctl00_ctl00_bcr_bcr_TextBox1_ErrorDiv" class="ValidatorErrorDiv" style="display:none">
                        <span id="ctl00_ctl00_bcr_bcr_TextBox1_Label1">You must enter a <br>number</span>
                    </div>
                </span>
            </td>
            <td>
                <span id="ctl00_ctl00_bcr_bcr_TextBox2" class="txtfill" style="display: inline-block;">
                    <input name="ctl00$ctl00$bcr$bcr$TextBox2$textbox1" class="txtSec80CCD" value="0" id="ctl00_ctl00_bcr_bcr_TextBox2_textbox1" style="width: 70px;" type="text">
                    <span id="ctl00_ctl00_bcr_bcr_TextBox2_valCustom" style="color: Red; display: none;">*</span>
                    <div id="ctl00_ctl00_bcr_bcr_TextBox2_ErrorDiv" class="ValidatorErrorDiv" style="display:none">
                        <span id="ctl00_ctl00_bcr_bcr_TextBox2_Label1">You must enter a <br>number</span>
                    </div>
                </span>
            </td>
        </tr>
        <tr>
            <td>
                <p>Life Insurance Premium</p><br>
            </td>
            <td>
                <span id="ctl00_ctl00_bcr_bcr_TextBox3" class="txtfill" style="display: inline-block;">
                    <input name="ctl00$ctl00$bcr$bcr$TextBox3$textbox1" class="txtSec80CCD" value="0" id="ctl00_ctl00_bcr_bcr_TextBox3_textbox1" onblur="calcDedSecCCD(this)"  style="width: 70px;" type="text">
                    <span id="ctl00_ctl00_bcr_bcr_TextBox3_valCustom" style="color: Red; display: none;">*</span>
                    <div id="ctl00_ctl00_bcr_bcr_TextBox3_ErrorDiv" class="ValidatorErrorDiv" style="display:none">
                        <span id="ctl00_ctl00_bcr_bcr_TextBox3_Label1">You must enter a <br>number</span>
                    </div>
                </span>
            </td>
            <td>
                <span id="ctl00_ctl00_bcr_bcr_TextBox4" class="txtfill" style="display: inline-block;">
                    <input name="ctl00$ctl00$bcr$bcr$TextBox4$textbox1" class="txtSec80CCD" value="0" id="ctl00_ctl00_bcr_bcr_TextBox4_textbox1" style="width: 70px;" type="text">
                    <span id="ctl00_ctl00_bcr_bcr_TextBox4_valCustom" style="color: Red; display: none;">*</span>
                    <div id="ctl00_ctl00_bcr_bcr_TextBox4_ErrorDiv" class="ValidatorErrorDiv" style="display:none">
                        <span id="ctl00_ctl00_bcr_bcr_TextBox4_Label1">You must enter a <br>number</span>
                    </div>
                </span>
            </td>
        </tr>

   ......
   ......
   ......

    </tbody>
</table>  

我想要文本框id:ctl00$ctl00$bcr$bcr$TextBox2$textbox1关于textbox1的更改事件。 如何使用类获取文本输入的id。
或者通过任何其他方式在更改第一个文本框时获取下一个文本框的id

您可以使用jquery索引函数

var $spans = $("td>span.txtfill");

// Your next function
function get_next_span(elem){

  var $span = $(elem).closest('td>span.txtfill'),
      index = $spans.index($span);

  return $spans.eq(index+1);
}
因此,如果您有一个文本字段,您将使用以下帮助器函数:

var $current = $("#ctl00_ctl00_bcr_bcr_TextBox2_textbox1"),
    $next = get_next_span($current).find('input[type=text]');   
或在变更事件中:

$("input[type=text]").change(function(){
   get_next_span(this).find('input[type=text]').show();
});

您编写的标记太难阅读。随着时间的推移,你会开始欣赏简短的ID、名称等。因为没有必要在每个选择器中使用40个字符,这只会使编写javascript和css变得困难,甚至以后更难阅读和理解代码

您还成功地创建了一个复杂的嵌套元素系统,这些嵌套元素几乎肯定是不必要的,并且很难用javascript进行遍历,保持其更简单通常是一个好主意

首先,您可能不应该将“id”作为变量传递给函数,因为“id”实际上在javascript中做了一些事情(它获取元素id,请参见下面的脚本)

如果仅在每个“td”内查找下一个输入元素,则以下内容将起作用,但对于“td”中的最后一个输入,查找下一个“tr”内的下一个输入将不起作用,等等


这里有一个

@gdoron:检查格式化的代码并给我一些解决方案!这仍然很难理解你们需要什么,你们能重新措辞吗?如图所示,我在PF值行中有两个文本框。我想将textbox1的输入值设置为textbox2。为此,我需要textbox2在textbox1的更改事件中的id。这同样适用于其余行。我正在呼叫calcDedSecCCD(id)更改textbox1。我得到第一个文本框的id,通过使用next()从这个id中我想要第二个文本框的id。希望你得到我想要的。。。
$("input[type=text]").change(function(){
   get_next_span(this).find('input[type=text]').show();
});
function calcDedSecCCD(elm) {
    var eid = elm.id;
    var nextId = $(elm).parents('td').next().find('input')[0].id;

    alert("First Element: "+eid);
    alert("Next Element: "+nextId);
}