2011-11-24 85 views
1

我需要括號中的數字,在本例中爲「14」,以便我可以將它發送到頁面上的JavaScript函數以提交,或者可以選擇「單擊」按鈕「當前周」?從WebBrowser中的HTML元素獲取屬性值

<form id="timetablesForm" action="timetablesController.jsp" method="post"> 
    <p> 
     <input type="button" name="Current week" value="Current week" onclick="javascript:void+displayTimetable('14')" /> 
     <input type="button" name="Next week" value="Next week" onclick="javascript:void+displayTimetable('15')" /> 
     <input type="button" name="Current semester" value="Semester 1" onclick="javascript:void displayTimetable('7;8;9;10;11;12;13;14;15;16;17;21')" /> 
    </p> 

function displayTimetable(selectdweeks) 

我正在使用Windows Phone,因此WebBrowser.Document不可用。我用下面的腳本來設置不同形式的值:

webBrowser1.InvokeScript("eval", "document.getElementById('loginform1').user.value = 'username';"); 

如何得到從HTML頁面的值?還有什麼其他功能可以撥打InvokeScript?如果你能指出我的功能列表,請欣賞它。

回答

2

使用此

webBrowser1.SaveToString() 

。然後解析HTML以獲得我想要的位

0

您是否試過這樣做,獲得價值?

Object val = webBrowser1.InvokeScript("eval", "document.getElementById('loginform1').user.value;"); 
+0

嘗試了許多變體的對象val = webBrowser1.InvokeScript(「eval」,「document.getElementById('timetablesForm')。'Current week'.onclick.value;」 );'但所有拋出一個SystemException – StanOverflow

1

下面的代碼從你的第一個輸入「當前周」提取「14」:

private void button1_Click(object sender, RoutedEventArgs e) 
{ 
    // first define a new function which returns the content of "onclick" as string 
    webBrowser1.InvokeScript("eval", "this.newfunc_getmyvalue = function() { return document.getElementById('Current week').attributes.getNamedItem('onclick').value; }"); 
    // now invoke the function and save the result (which will be "javascript:void+displayTimetable('14')") 
    string onclickstring = (string)webBrowser1.InvokeScript("newfunc_getmyvalue"); 
    // now split the string at the single quotes (you might have to adapt this parsing logic to whatever string you expect; this is just a simple approach) 
    string[] substrings = Regex.Split(onclickstring, "'"); 
    // this shows a messagebox saying "14" 
    MessageBox.Show(substrings[1]); 
} 

一點背景:

起初我以爲返回的值應該是一樣容易如:

// ATTENTION: THIS DOESN'T WORK 
webBrowser1.InvokeScript("eval", "return 'hello';"); 

但這始終引發SystemException(「未知錯誤 已經發生了。錯誤:80020101.「)這就是爲什麼上面的第一代碼定義了一個JavaScript函數返回所需的值的原因然後我們調用這個函數,結果成功返回

+0

第二個強制轉換的字符串行不斷拋出相同的異常「錯誤:80020101.」是否可以「單擊「這個方法的按鈕」當前周「?我需要14或點擊它。非常感謝 – StanOverflow

+0

奇怪。對我來說它有效。你在運行芒果嗎?試試'webBrowser1.InvokeScript(「eval」,「document.getElementById('Current week')。click()」);'點擊。 –

+0

應用程序的目標是WP OS 7.1,並且該代碼也爲我提出了一個系統錯誤:( – StanOverflow