Issue while store the javascript textbox value int

2019-08-30 01:43发布

问题:

I may be missing something obvious here, but how could I rewrite this code!

i am trying here to store the value entered in the textbox(Textbox was showed in javascript dialog page).In a javascript dialog page i have one 'ok' button.. when i click the button i want to store the value entered in the textbox.I want to save the content using Ajax.

Please see my sample code View page:

<script language="javascript" type="text/javascript">
    $(function () {
        $('.button').live('click', function () {
            $('.Text_dialog').dialog('open');
        });

        $('.Text_dialog').dialog({
            autoOpen: false,
            buttons: {
                'Ok': function () {
                    var textValue = $(':txtValue').val();
                    $.ajax({
                        url: '/Home/About',
                        type: 'POST',
                        data: { str: textValue },
                        success: function (result) {
                            alert(result.val);
                        }
                    });
                },
            }
        });
    });

</script>


     <input type="button" value="Add Value" class="button" />
     <div class="Text_dialog" title="Basic modal dialog">
          TextValue: <input type="text" class="txtValue" />
</div>

Control page:

[HttpPost]
    public ActionResult About(string str)
    {
        ValidateClass ObjAM = new ValidateClass();
        int value = ObjAM.ValidatetextValue(str);
        return Json(new { val = value });
    }

Model page:

 public class ValidateClass
    {
        DataClasses1DataContext dbObj = new DataClasses1DataContext();
        public int ValidatetextValue(string str)
        {
            string value = (from SearchtextValue in dbObj.Options
                                where SearchtextValue.OptionName == str
                                select SearchtextValue.OptionName).Single();
            if (value == null)
            {
                return 1;
            }
            return 0;

        }
    }

When i run this code i am getting script error like "Object doesn't support this property or method".Please advice

回答1:

Change data: { str: textValue } to data: "{ 'str' :'" + textValue +"' }"

If you are confused on escaping the quotes properly, try this.

$(function() {
    $('.button').live('click', function() {
        $('.Text_dialog').dialog('open');
    });

    $('.Text_dialog').dialog({
        autoOpen: false,
        buttons: {
            'Ok': function() {
                var DTO = "{ 'str' :'" + $('.txtValue').val() +"' }";
                $.ajax({
                    url: '/Home/About',
                    type: 'POST',
                    data: DTO,
                    success: function(result) {
                        alert(result.val);
                    }
                });
            },
        }
    });
});

Please note that I have changed the :txtValue to .txtValue as it is the correct selector.



回答2:

can you try changing

<input type="text" class="txtValue" />

to

<input type="text" class="txtValue" id="txtValue" />

and

var textValue = $(':txtValue').val();

to

var textValue = $('#txtValue').val();

I've never used : selector... but I think it will return you a collection and you should use index [0] or, why don't you use class selector .?

Edit: as I see you cannot use : in this case

also you can add dataType: "json",into your Ajax request, and change your method return type from

public ActionResult About(string str)

to

public JsonResult About(string str)

EDIT 2:

have you included the library where dialog() function is? after including the jquery library?