Javascript IF在确认脚本中

时间:2014-09-05 15:18:51

标签: java javascript vb.net

在我的服务器端页面上,我有一段遗留脚本要修改,以便在打开确认窗口之前测试字段是否为空。这是我尝试的,添加$(#hdfldRecId)因为如果此字段为空,我不希望确认打开。

    scriptBlock = "function beforeDelete()" & vbCrLf & _
                      "{return($('#hdfldRecId').val() !> '' && confirm('Are you sure you want to delete this item?'));}"
If (Not ClientScript.IsClientScriptBlockRegistered("deletePromptScript")) Then
            ClientScript.RegisterClientScriptBlock(Me.GetType(), _
                                                   "deletePromptScript", _
                                                   scriptBlock, _
                                                   True)
End If

    'use the OnClientClick property of the button to cause the above
    'script to be executed when the button is clicked

btndelete.OnClientClick = "return(beforeDelete());"

当我执行页面时,调试表明缺少()但配对都匹配。

<script type="text/javascript">
//<![CDATA[
function beforeDelete()
{return($('#hdfldRecId').val() !> '' && confirm('Are you sure you want to delete this item?'));}//]]>
</script>

所以我不确定如何添加此测试以查看$(hdfldRecId)中是否有值,如果有,则执行确认对话框。

谢谢

2 个答案:

答案 0 :(得分:1)

return不是一种方法,所以摆脱(和)。

"return beforeDelete();" 

并且不确定是什么!&gt;应该是。猜猜你要检查长度

function beforeDelete() { 
    return $('#hdfldRecId').val().length && confirm('Are you sure you want to delete this item?');
}

答案 1 :(得分:0)

你试过了吗?

btndelete.OnClientClick = "beforeDelete;"

或者确实:

btndelete.OnClientClick = "return beforeDelete();"

除了修复epascarello指出的错字。