1

鉴于此 div:

<div id="addActor1">
    <select type="select" id="ddlSelectActor1" class="selectActor">

    </select><br />
    <br />
    <p class="newActor">
        <label id="lblAddActor1FirstName" for="txtAddActor1FirstName">First Name: </label>
        <input type="text" id="txtAddActor1FirstName" size="20" />
        <label id="lblAddActor1LastName" for="txtAddActor1LastName">Last Name: </label>
        <input type="text" id="txtAddActor1LastName" size="20" />
    </p>
</div>

When a particular option in the select (which is populated in js file) is selected, I need to check if either of the inputs has anything entered in them.

我尝试了以下和类似的方法,但无法完全弄清楚:

$('#addActorsDialog select.selectActor').each(function() {
    var empty = true;

    // This gives me js error in console: "Uncaught Error: Syntax error, unrecognized expression: p input:nth-child(2)]"
    console.log('p html: ' + $(this).parent().find('p input:nth-child(2)]').text());

    if ($(this).val() !== '0' ||
            ($(this).val() === 'a'
                // || [if textboxes are empty]
                )
            ) { empty = false; }
    //...
});

如何在此处检查任一输入是否有值?

4

2 回答 2

4

我认为你需要删除]

find('p input:nth-child(2)' )

并在此处检查任一输入是否具有值,您可以执行以下操作:

var emptyInputs = $(this).parent().find('p input[type="text"][value!=""]');

if(emptyInputs.length > 0) {
   empty = false;
} else {
   empty = true;
}

或者

var emptyInputs = $(this).parent().find('p input[type="text"]').filter(function() {
                       return $.trim( this.value ).length > 0;
                  });
于 2013-02-18T16:48:01.170 回答
0

尝试这个:

console.log('p html: ' + $(this).parent().find('p input:nth-child(2)')[0].text());

这将为您提供返回的列表中第一个元素的文本值find

于 2013-02-18T16:48:45.127 回答