将值标记添加到html元素

时间:2017-05-02 21:05:53

标签: c# winforms webbrowser-control htmlelements

Picture In Response to a Answer What The Webpage looks Like

<input name="ftitle" class="inputbox ui-autocomplete-input" type="text" autocomplete="off">

我需要将value =添加到上面的HTML输入元素中 我该怎么做呢?对于我的生活,我无法弄明白。

编辑:我需要这个,因为我需要填写一个不包含值=的网页上的文本框。但如果我右键单击它并添加属性值=那么我可以通过我的程序更改文本。

我正在使用C#网页浏览器控件,所以我正在使用

HtmlElement NewAttribute = doc.GetElementById("ftitle");

因此,当所有事情都说完了,它就会像这样。

<input name="ftitle" class="inputbox ui-autocomplete-input" type="text" 
 autocomplete="off" value="">

这是我到目前为止所得到的,如果它有助于了解我的目标。这是一个不同的网页,但我做同样的事情,但我需要添加类&#34;值&#34;

 private void Generate_Click(object sender, EventArgs e)
    {




        HtmlDocument doc = wbNewProject.Document;



        HtmlElement wbJobName = doc.GetElementById("Name"); //lblcontact.text
        HtmlElement wbEngineer = doc.GetElementById("engineer-lookup"); //
        HtmlElement wbSalesEng = doc.GetElementById("SalesEngineerUserId");
        HtmlElement wbLocation = doc.GetElementById("Location");
        HtmlElement wbBidDate = doc.GetElementById("BidDate");
        HtmlElement wbPriorApproval = doc.GetElementById("PriorApproval"); //True or False
        HtmlElement wbTakeOff = doc.GetElementById("TakeOffComplete"); //True or False
        HtmlElement wbProject = doc.GetElementById("RoleType"); //Design/Build or Plan/Spec
        HtmlElement element = wbNewProject.Document.GetElementById("ftitle");




        try
        {
            wbJobName.SetAttribute("value", lblJobName.Text);

            if (lblContact.Text.Contains("Dan"))
                wbSalesEng.SetAttribute("value", "2");
            if (lblContact.Text.Contains("Kelley"))
                wbSalesEng.SetAttribute("value", "3");
            if (lblContact.Text.Contains("Erv"))
                wbSalesEng.SetAttribute("value", "4");
            if (lblContact.Text.Contains("Marc"))
                wbSalesEng.SetAttribute("value", "5");
            if (lblContact.Text.Contains("Terry"))
                wbSalesEng.SetAttribute("value", "6");
            if (lblContact.Text.Contains("Chad"))
                wbSalesEng.SetAttribute("value", "7");
            if (lblContact.Text.Contains("Jacob Lenertz"))
                wbSalesEng.SetAttribute("value", "10");
            if (lblContact.Text.Contains("Terry"))
                wbSalesEng.SetAttribute("value", "11");
            if (lblContact.Text.Contains("Nate"))
                wbSalesEng.SetAttribute("value", "12");

            wbLocation.SetAttribute("value", lblLocation.Text);
            wbBidDate.SetAttribute("value", lblBidDate.Text);

            if (lblPriorApp.Text.Contains("Yes"))
                wbPriorApproval.SetAttribute("value", "true");
            if (lblPriorApp.Text.Contains("No"))
                wbPriorApproval.SetAttribute("value", "false");
            if (lblTakeOff.Text.Contains("Done"))
                wbTakeOff.SetAttribute("value", "true");
            if (lblTakeOff.Text.Contains("Not Done"))
                wbTakeOff.SetAttribute("value", "false");

            wbEngineer.SetAttribute("value", lblEngineer.Text);
            wbProject.SetAttribute("value", lblProject.Text);
        }
        catch { }
    }

2 个答案:

答案 0 :(得分:0)

的Javascript

document.getElementById(elementName).value = "your value";

的jQuery

$('#elementName').val() = "your value";

这两种方法都要求您在元素上创建 Id 。但是,jQuery还允许您按other means other than an Id选择元素。

Web表单c#

这也很简单,请使用SetAttribute Method

HtmlElement element = doc.GetElementById("ftitle"); //Your have this, correct?

element.SetAttribute("value", "your value");

答案 1 :(得分:0)

你可以做到这种令人难以置信的好办法:

$(document).ready(function() {
     var title = @model.Title;
     $('[name="title"]').val(title);
});

您也可以直接实施Razor:

<input type="text" value="@model.Title" />

请记住,如果@model为null,您将收到应用程序错误。但如表示的那样,这是一种方法。我诚实地采用的方法是JavaScript模板,然后将我的数据模型直接解析到模板,然后将模板直接附加到视图状态。