【问题标题】:How to Check if other page field value is empty using jquery如何使用jquery检查其他页面字段值是否为空
【发布时间】:2014-09-26 03:54:04
【问题描述】:

我正在从 Mysql db 获取 所有联系人,然后将其显示在 html 行上。每一行都有onClick() 方法 点击每一行后会在所有联系人右侧显示联系人详情。 p>

显示所有联系人:

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>

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

【问题讨论】:

  • 您可以这样做:将onchange 处理程序添加到注释框。在该处理程序中,为文本字段设置一个自定义属性,例如$(notebox).attr('change','yes')。然后在您的联系人列表的onclick 中,在您更改任何内容之前,检查notebox 是否具有change=yes 属性并显示您的消息。
  • @Michel 感谢您的回复。你能告诉我具体怎么做吗?

标签: javascript jquery confirm


【解决方案1】:

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?
}   

【讨论】:

    【解决方案2】:

    在这里您可以使用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
    

    【讨论】:

      猜你喜欢
      • 2011-05-13
      • 1970-01-01
      • 2021-08-26
      • 2011-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多