问题描述
我想创建一个按钮,当单击它时,将弹出甜蜜警报,并且其中有文本框,我想获取文本框的值,然后通过ajax将其发送到我的Action中,发生? 像下面的代码
$("btn").click(function(){
swal({
title:'Please text the number',text:'You can text more than one',content:"input",showcancelbutton:'true',button{
text:"Search"
}
}),function(InputData)
{
if(InputData === ""){
swal('Error','You Need To Write Something','error')
}
$.ajax({
url:@Url.action("MyActionName","MyControllerName"),method:"Post",data: InputData
})
}
})
解决方法
我从swal网站获得了一个完整的示例。遵循相同的步骤,您会得到它:
swal({
text: 'Search for a movie. e.g. "La La Land".',content: "input",button: {
text: "Search!",closeModal: false,},})
.then(name => {
if (!name) throw null;
return fetch(`https://itunes.apple.com/search?term=${name}&entity=movie`);
})
.then(results => {
return results.json();
})
.then(json => {
const movie = json.results[0];
if (!movie) {
return swal("No movie was found!");
}
const name = movie.trackName;
const imageURL = movie.artworkUrl100;
swal({
title: "Top result:",text: name,icon: imageURL,});
})
.catch(err => {
if (err) {
swal("Oh noes!","The AJAX request failed!","error");
} else {
swal.stopLoading();
swal.close();
}
});