jq html t替换,javascript - innerHTML works jQuery html() doesn't - Stack Overflow

For some reason I can't get jQuery html() function to work. I've tried the following:

Find a specific div:

var div = $("#divId");

Outcome: works

Test that i can use it with jQuery:

alert($(div).attr("id"));

Outcome: works; the id is presented

Get the html within the div:

alert($(div).html());

Outcome: does not work; empty alert

Get the html within the div with innerHTML:

alert(document.getElementById($(div).attr("id")).innerHTML);

Outcome: works; the div content is presented in the alert

The actual code:

$(document).ready(function(){

var autocomp = namespace('mx.autocomp');

autocomp.overlay = (function() {

var formId;

var $searchValueComp;

var $searchResultComp;

function init(inFormId, inSearchValue){

formId = inFormId;

$searchValueComp = $("#"+inFormId).find("[id$="+inSearchValue+"]");

$searchResultComp = $("#"+inFormId).find("[id$=searchResult]");

}

function handleOverlay(){

var fn = window[formId + "OverlayWid"];

var result = document.getElementById($searchResultComp.attr("id")).innerHTML;

if($searchValueComp.val().length==0){

fn.hide();

}

// Test - This does not work as I get an empty alert

alert($searchResultComp.html());

// Edit1: New test, this works.

// When I use this javascript, I start with initializing the script from the page

// using the "init(inFormId, inSearchValue)" function. The handleOverlay() function

// is called through the "oncomplete='mx.autocomp.overlay.handleOverlay();'" of a

// p:remoteCommand that executes a search in the db and then update the div called

// "searchResultComp".

//

// Only reason I can think of why the code below works is that the jQuery object $test

// is created after the div called "searchValueComp" has been updated by p:remoteCommand.

// However, I don't understand why the jquery object "searchValueComp" wouldn't have

// access to the same content as it should look for it first when the html() function

// is called. Or is the content of the div searchValueComp set immediately when the

// object is created in the "init" function?

var $test = $("#"+formId).find("[id$=searchResult]");

alert($test.html());

// I need to check if the div: "searchResultComp" has any content.

// As I don't get $searchResultComp.html() to work, I'm forced to

// use the "getElementById" way instead.

if(result.length==0){

fn.hide();

}else{

fn.show();

}

}

function print(text){

$("#textCheck").prepend(text + '\n');

}

return {

handleOverlay: handleOverlay,

init: init

};

})();

});

What am I doing wrong?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,使用T-SQL和HTML来实现密码格式验证的步骤如下: 1. 在注册页面中,添加一个密码输入框和一个用于显示错误消息的文本框。 ```html <label for="password">密码:</label> <input type="password" id="password" name="password" required> <br> <span id="passwordError" style="color: red;"></span> ``` 2. 在表单提交时,使用T-SQL语句来验证密码格式,并在出现错误时将错误消息显示在密码输入框下方的文本框中。例如: ```html <form method="post" action="register.php"> <label for="password">密码:</label> <input type="password" id="password" name="password" required onchange="validatePassword()"> <br> <span id="passwordError" style="color: red;"></span> <br> <input type="submit" value="注册"> </form> <script> function validatePassword() { var password = document.getElementById("password").value; if (password.length < 6 || password.length > 10) { document.getElementById("passwordError").innerHTML = "密码长度应该在6到10位之间。"; return false; } else { document.getElementById("passwordError").innerHTML = ""; return true; } } </script> ``` 以上代码中,我们使用onchange属性在密码输入框的值发生改变时调用validatePassword()函数进行密码格式验证。如果验证失败,我们将在错误消息文本框中显示相应的错误消息。否则,我们将清空错误消息文本框。 在后台的register.php页面中,可以使用T-SQL语句来验证密码格式或进行其他的表单验证,并将结果返回给前端页面进行显示。 注意:在使用T-SQL进行表单验证时,需要注意防止SQL注入攻击。可以使用参数化查询或其他有效的防护措施来提高安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值