0

在我的 php 项目中,我有一个 javascript 变量说“addText”,其中包含以下 html 内容

<div class="foxcontainer" style="width:100% !important;">
<h2>FREE IN HOME CONSULTATION</h2>

<ul class="fox_messages">
    <li>Invalid field: Your name</li>
    <li>Invalid field: Your email</li>
</ul>

<form enctype="multipart/form-data" class="foxform" action="/hima/bigriglawgroup/index.php/about-us#mid_131" method="post">
<!-- mod_foxcontact 2.0.19 GNU/GPLv3 -->
<div class="foxfield">
    <input class="invalidfoxtext" value="Your name" title="Your name" style="width:85% !important;display:block;float:none;margin:0 auto !important;" name="_c31f7a92d344f9a5c23d07fd438ba0b6" onfocus="if(this.value==this.title) this.value='';" onblur="if(this.value=='') this.value=this.title;" type="text"> 
    <span class="asterisk"></span>
</div>
<div class="foxfield">
    <input class="invalidfoxtext" value="Your email" title="Your email" style="width:85% !important;display:block;float:none;margin:0 auto !important;" name="_4fd8a7bb907c63baca708086b63eb7f0" onfocus="if(this.value==this.title) this.value='';" onblur="if(this.value=='') this.value=this.title;" type="text"> 
    <span class="asterisk"></span>
</div>
<div class="foxfield">
    <input class="validfoxtext" value="Phone number" title="Phone number" style="width:85% !important;display:block;float:none;margin:0 auto !important;" name="_68a05eb930e9ce854ed0c2d2d25c14a1" onfocus="if(this.value==this.title) this.value='';" onblur="if(this.value=='') this.value=this.title;" type="text">
</div>
<div class="foxfield">
    <textarea rows="" cols="" class="validfoxtext" name="_0ef408e84316ef5737db72a727579f48" title="Notes" style="width:85% !important;height:180px !important;display:block;float:none;margin:0 auto !important;" onfocus="if(this.value==this.title) this.value='';" onblur="if(this.value=='') this.value=this.title;">retret ret ret ret</textarea>
</div>
<div class="foxfield">
    <button class="foxbutton" type="submit" style="margin-right:32px;" name="mid_131">
    <span>Submit</span>
    </button>
</div>
<div class="fox-copyright" style="padding:10px 0 !important;text-indent:0 !important">
    <a target="_blank" title="Joomla contact form" href="#" style="display: none !important; display: inline !important; font-size:10px !important;">powered  by fox contact</a>
</div>

</form>

</div>

在这里,我想检查该内容中是否存在字符串“无效字段”。如果它不存在,我想做一些其他功能。但我不知道该怎么做

有人知道吗?请帮助我ASPS。

提前致谢

4

3 回答 3

1
if (addText.indexOf("Invalid field") != -1) { /* it's present */ }

// OR with regex

if (/Invalid field/.test(addText)) { /* it's present */ }

// OR with case-insensitve regex:

if (/Invalid field/i.test(addText)) { /* it's present */ }
于 2012-11-30T11:33:01.347 回答
0

你可以使用strstr,文档@http ://www.php.net/manual/en/function.strstr.php

谢谢

于 2012-11-30T11:33:37.483 回答
0

我真的不推荐用正则表达式解析 html,但是在 vanilla javascript 中没有简单的方法。
如果您使用 jQuery,那么:contains选择器的任务非常简单:

var toSearch = "Invalid field"
    elements = $(':contains('+ toSearch +')');

// if we have any elements that contain the searched keywords
if(elements.length) {
    // do smth
}
于 2012-11-30T11:44:13.940 回答