I have this code :
jQuery('#btSave').click(function (event) {
var jqxhr = $.post("Controller/Action", {
lastName: $("#LastName").val()
},
function (data) {
//her开发者_如何转开发e
})
});
I'd like to know if in the "data" variable the id "Mydiv" exist
How can I don this ?
Thanks,
You can i either use RegEx or make use of String Manipulation function like IndexOf...
or
try search() String method.
var isFound = data.search(new RegExp(/Mydiv/i));
if you are using jQery than here is alredy given answer
jQuery .search() to any string
Find text string using jQuery?
I assume data
is a string containing HTML markup. If so, then:
var tree = $(data);
if (tree.find("#Mydiv")[0]) {
// An element with the `id` "Mydiv" exists in the tree
}
You don't have to use a variable, you could just do this:
if ($(data).find("#Mydiv")[0]) {
// An element with the `id` "Mydiv" exists in the tree
}
...but I just assume you're probably going to want to do something else with tree
afterward, so we want to avoid parsing it more than once.
Note: If the "Mydiv" element may be at the top level of the HTML in data
, you'll want to wrap that structure in a parent element for the above to work (there are other ways, but they're more complicated). So if that may be the case, use $("<div>" + data + "</div>")
rather than just $(data)
. This is because find
searches descendant elements, so if the HTML were "hi", it wouldn't find it as it's not a descendant (it's the element itself).
Update: Here's a live example
This will do if i understood correctly.
if($("#Mydiv",data).length)
{
//Mydiv exists
}
To the extent of my understanding...
if you are trying to find div with id 'myDiv' in data... you can do this..
function (data) {
if($("#Mydiv",data)) {
// this should do
}
})
Be little more clear in explaining what you want....
var result="limitless isa web ...";
if(result.search('limitless')>=0) // or another text Example: web
{
alert("ok");
}
精彩评论