如何使用jquery检查其他页面字段值是否为空

时间:2014-09-26 03:54:04

标签: javascript jquery confirm

我从Mysql db获得所有联系人,然后在html行显示它。每行都有onClick()方法点击每个后,它会在所有联系人的右侧显示联系详情

显示所有联系人:

echo "<tr onclick='getDetails($cdid), showthirdbox($cdid), visited(this);'>";                               
echo "<td class='' valign='top' align='left' width='20'>$companyName</td>";
echo "<td class='' valign='top'>$family_name</td>";
echo "<td class='' valign='top'>$given_name</td>";
echo "<td class='' valign='top'>$department</td>";
echo "<td class='' valign='top'>$title</td>";    
echo "</tr>";

getDetails()调用getContactDetails.php(联系人详细信息)页面。在这个页面中,我有一个名为Note的字段,用户可以在其中输入注释。

所以现在我想向用户显示确认消息仅当用户在Enter Note框上写一些内容但是点击其他(所有联系人)时保存此Enter Notes

确认问题将是:您是否要在转到其他联系人之前保存此便笺框?是或否。如果是,则调用saveNote(),否则将加载新的联系人行。

我不知道如何使用Jquery或Javascript执行此操作?你有什么想法或解决方案吗?谢谢。

getDetails()函数:

function getDetails(id) {
            id = id;
            $.ajax({
            type:"post",
            url:"getContactDetails.php",
            data:"id="+id,
            success:function(res){
                $('#visiable').show();
                $('#notebox_visiable').show();
                $('#thirdBox').show();

                $('#all_contact_details').html(res);
                showthirdbox(id);
                //showNotexBox(id);
                //showAllNotesBox(id);
                }
           });
}

getContactDetails.php页面有Note字段:

<tr>
    <td colspan="2">Enter Note<p id="demo"></p></td>
</tr>
<tr>
    <td colspan="2">        
    <center><img id="loading-image" src="images/loading-image.gif" style="display:none; margin-bottom:10px !important;"/></center>
    <div id="Notesboxsuccess"></div>
    <form action="" method="post" id="showNotesBox" name="notebox">      
    <input type="hidden" value="<?php echo $id; ?>" name="cdid" id="cdid"/>
<tr>
    <td colspan="2"><textarea name="contentText" id="contentText"  cols="35" rows="4" placeholder="enter note"></textarea></td>
</tr>
<tr>
    <td colspan="2">&nbsp;</td>
</tr>
<tr>
    <td colspan="2"><input type="submit" value="Save" name="submit" class="submit" id="addNewNotes"/></td>
</tr>
    </form>

点击一行后的主页视图(所有联系人):

enter image description here

2 个答案:

答案 0 :(得分:0)

change仅在模糊时触发,因此只要用户停留在文本框中,就无需执行任何操作。

jQuery change()

附加更改事件处理程序:

$("#contentText").change(function() {
    $("#contentText").attr("ididwritesomething","yep");
    }

在保存按钮中删除属性jQuery removeAttr()

$("#contentText").removeAttr("ididwritesomething");

在联系人列表的点击处理程序中,检查属性是否设置为(from this question)

var attr = $("#contentText").attr("ididwritesomething");
if (typeof attr !== typeof undefined && attr !== false && attr=="yep"){
       //do you want to save?
}   

答案 1 :(得分:0)

您可以使用jQuery Change功能执行此操作。 例如,

<input id="field" type="text" value="abcd">
<a id="pid" href="#">Link</a>

对于上面的html代码,jQuery代码如下:

$(document).ready(function(){
$("#pid").click(function(){
  $("#field").change(function(){
    alert("The text has been changed.");
  });// end of change
});// end of click function
});//end of document