使用jQuery检查人员选择器字段是否为空?

时间:2010-08-16 02:56:30

标签: jquery

我有以下HTML

    <tr>
<td>
<div id="ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl00_ctl04_ctl08_ctl00_ctl00_ctl04_ctl00_ctl00_UserField_upLevelDiv" TabIndex="0" onFocusIn="this._fFocus=1;saveOldEntities('ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl00_ctl04_ctl08_ctl00_ctl00_ctl04_ctl00_ctl00_UserField_upLevelDiv')" onClick="onClickRw(true, true);" onChange="updateControlValue('ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl00_ctl04_ctl08_ctl00_ctl00_ctl04_ctl00_ctl00_UserField')" onFocusOut="this._fFocus=0;" onPaste="dopaste();" AutoPostBack="0" class="ms-inputuserfield" onDragStart="canEvt(event);" onKeyup="return onKeyUpRw('ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl00_ctl04_ctl08_ctl00_ctl00_ctl04_ctl00_ctl00_UserField');" onCopy="docopy();" onBlur="updateControlValue('ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl00_ctl04_ctl08_ctl00_ctl00_ctl04_ctl00_ctl00_UserField')" Title="People Picker" onKeyDown="return onKeyDownRw(this, 'ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl00_ctl04_ctl08_ctl00_ctl00_ctl04_ctl00_ctl00_UserField', 3, true, event);" contentEditable="true" style="width: 100%; word-wrap: break-work;overflow-x: hidden; background-color: window; color: windowtext;" name="upLevelDiv">
</div>
<textarea name="ctl00$m$g_c6ae303a_6013_4adb_8057_63a214bcfd24$ctl00$ctl04$ctl08$ctl00$ctl00$ctl04$ctl00$ctl00$UserField$downlevelTextBox" rows="1" cols="20" id="ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl00_ctl04_ctl08_ctl00_ctl00_ctl04_ctl00_ctl00_UserField_downlevelTextBox" class="ms-input" onKeyDown="return onKeyDownRw(this, 'ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl00_ctl04_ctl08_ctl00_ctl00_ctl04_ctl00_ctl00_UserField', 3, true, event);" onKeyUp="onKeyUpRw('ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl00_ctl04_ctl08_ctl00_ctl00_ctl04_ctl00_ctl00_UserField');" Title="People Picker" AutoPostBack="0" style="width:100%;display: none;position: absolute; ">
</textarea></td>
                            </tr>

我正在尝试检查人员选择器字段是否有任何值,但我没有让它工作(不能使用ID属性来查找元素),我不确定要检查哪个元素。

我目前的代码是:

$("input[title=Target Date],input[title=Start Date],select[title=Strategic Objective],select[title=Strategic Priority]").change(function(){
        checkControls()
});

在这里我想添加类似的内容 ,选择[title = Strategic Priority],textarea.ms-input“)但它不起作用,有什么想法吗?

提前致谢。

编辑:验证的代码是:

    //bind a change event to all controls to validate
    $("input[title=Target Date],input[title=Start Date],select[title=Strategic Objective],textarea[id$=_UserField_downlevelTextBox],select[title=Strategic 

Priority]").change(function(){
        checkControls()
    });

    //the change event function - check the status of each control
    function checkControls(){

    //set a variable to count the number of valid controls
    var controlsPassed = 0;

    //set up a selector to pick .each() of the target controls
    $("input[title=Target Date],input[title=Start Date],select[title=Strategic Objective],textarea[id$=_UserField_downlevelTextBox],select[title=Strategic 

Priority]").each(function(){

        //if the control value is not zero AND is not zero-length
        var val = $(this).val();
        if($(this).is(':hidden') || (val != 0 && val.length != 0)) { 

            //add one to the counter
            controlsPassed += 1;

        }
        });

    //call the PreSaveItem function and pass the true/false statement of 5 valid controls
    return (controlsPassed == 5);

    }

        function PreSaveItem() {
            return checkControls()
    }

edit2:我猜它不起作用,因为其他控件都有一个value属性。如果我在日期字段中插入某些内容,则会获得属性值=“18/08/2010”。但是对于textarea,html看起来像(简化)值,所以如何检查它是否为空?

EDIT3:

我改变了

return (controlsPassed == 5);

return (controlsPassed == 4) && ( ($("textarea[title='People Picker'][value!='']").length==1) || $("textarea[title='People Picker'][value!='']").is(":hidden") );

但如果人物选择器被隐藏(.hide()),它仍然想要验证它,所以我想我可以做类似的事情:

if ($("textarea[title='People Picker'][value!='']").is(":hidden")) {
    return (controlsPassed == 4)
}
else {
return (controlsPassed == 4) && ( ($("textarea[title='People Picker'][value!='']").length==1);

但不,有什么想法吗?     }

4 个答案:

答案 0 :(得分:0)

您添加textarea.ms-input的想法是正确的 - 这将导致更改textarea的值以通过checkControls()方法运行。但是,由于这是您在更改处理程序中调用的唯一方法,因此您必须在该方法中执行空检查,或者在调用之前执行空检查。

您可以在此simple example中看到如何检查空文本区域:

$('textarea.ms-input').change(function(){
    if($.trim(this.value) == ''){
        alert("Look maw, no content!");   
    }
});

答案 1 :(得分:0)

如果您的PeopleEditor的ID为 UserField ,则以下代码应该可以为您选择textarea。

$(textarea['id$=_UserField_downlevelTextBox'])

下面的代码片段将是您在代码中的内容:

$(textarea['id$=_UserField_downlevelTextBox']).change(function(){
        checkControls();
});

答案 2 :(得分:0)

人员选择器首先将值存储在隐藏字段中,其id为Userfield +“hiddenSpanData”的id。

如果该字段为空,则检查此信息将告知您。

e.g。

if($.trim($('input[id$="ff161_ctl00_ctl00_UserField_hiddenSpanData"]').val()) == ""){
    return 'Please enter user name';
}

答案 3 :(得分:0)

 var user = $("#" + '<%= this.MyPPEControl1.ClientID %>').text();    
      var IsValidUser = user.search("@");
      if (IsValidUser > 0)
      {
            $('#' + '<%= this.MyPPEControl1.ClientID %>').css({"border": "", "background": ""  }); 
      }
      else
      {
           $('#' + '<%= this.MyPPEControl1.ClientID %>').css({ "border": "1px solid red", "background": "#FFCECE"
          });       
        return false;
      }